<?php

namespace App\System\Base\Services;

/**
 * 给用户发送审核通知模板消息类
 * Class Service
 * @package App\Services
 */
class SendNoticeService
{
    //存放实例对象
    private $appid;     //微信小程序appid
    private $app_secret; //微信小程序密钥
    private $template_id; //模板ID

    //给用户发送通知
    public function sendNotice($open_id, $page, $form_id, $status, $remark = '')
    {
        //YW368LlT8fXGQeLSUEbdW-kL9kq-5q-n6EGFhdSo_gE
        $access_token = $this->get_accessToken();
        $touser = $open_id;
        $this->template_id = 'YW368LlT8fXGQeLSUEbdW-kL9kq-5q-n6EGFhdSo_gE';
        /*
         * 根据活动id，获取活动对应的地址信息，$keyword3  -xzz 0705
         */
        $value = array(
            "keyword1" => array(
                "value" => '门店认证审核',
                "color" => "#4a4a4a"
            ),
            "keyword2" => array(
                "value" => $status,
                "color" => "#9b9b9b"
            ),
            "keyword3" => array(
                "value" => date('Y-m-d H:i:s', time()),
                "color" => "#9b9b9b"
            ),
            "keyword4" => array(
                "value" => $remark ?? '无',
                "color" => "#9b9b9b"
            )
        );

        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $access_token;
        $dd = array();
        $dd['touser'] = $touser;
        $dd['template_id'] = $this->template_id;
        $dd['page'] = $page;  //点击模板卡片后的跳转页面，仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。
        $dd['form_id'] = $form_id;  //获取的wxml表单form的id

        $dd['data'] = $value;                        //模板内容，不填则下发空模板

        $dd['color'] = '';                        //模板内容字体的颜色，不填默认黑色
        $dd['emphasis_keyword'] = '';    //模板需要放大的关键词，不填则默认无放大
        /* curl_post()进行POST方式调用api： api.weixin.qq.com*/
        $result = $this->https_curl_json($url, $dd, 'json');
        return $result;
    }

    /* 调用微信api，获取access_token，有效期7200s -xzz0704 */
    function get_accessToken()
    {
        /* 在有效期，直接返回access_token */
        if (\Session::get('access_token')) {
            return \Session::get('access_token');
        } /* 不在有效期，重新发送请求，获取access_token */
        else {
            $this->appid = config('global.s_appid');
            $this->app_secret = config('global.s_app_secret');
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->appid . '&secret=' . $this->app_secret;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($ch);
            curl_close($ch);
            $jsoninfo = json_decode($output, true);
            $access_token = $jsoninfo["access_token"];

            if ($access_token) {
                \Session::put('access_token', $access_token, 7100);
                return \Session::get('access_token');
            } else {
                return 'api return error';
            }
        }
    }

    /* 发送json格式的数据，到api接口 -xzz0704  */
    function https_curl_json($url, $data, $type)
    {
        if ($type == 'json') {//json $_POST=json_decode(file_get_contents('php://input'), TRUE);
            $headers = array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");
            $data = json_encode($data);
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $output = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Errno' . curl_error($curl);//捕抓异常
        }
        curl_close($curl);
        return $output;
    }

}